package com.example.controller;

import com.example.entity.Book;
import com.example.entity.Resp;
import com.example.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("books")
public class BookController {
    @Autowired
    private BookServiceImpl bookService;
    /**
     * 查询全部
     */
    @GetMapping("all")
    public Resp selectAll(){
        return new Resp(true,bookService.getAll());
    }

    /**
     * 根据书名或者作者姓名进行查询
     */
    @GetMapping("byCondition")
    public Resp selectByNameOrAuthor(String name,String author){
        System.out.println(name+","+author);
        return new Resp(true,bookService.getByNameOrAuthor(name,author));
    }

    /**
     * 查询书名和库存量，拼成对象数组返回给前端
     */
    @GetMapping("test")
    public @ResponseBody
    Object[] getBookNameAndNumbers(){
        List<Book> bookList = bookService.getNameAndNumbers();
        Object[] objects = new Object[2];

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();
        for (Book a: bookList) {
            list1.add(a.getBookNumber());
            list2.add(a.getBookName());
        }
        objects[0]=list1;
        objects[1]=list2;
        return objects;
    }
}
